package net.yc.web.imalachat.push.service;

import com.google.common.base.Strings;
import net.yc.web.imalachat.push.bean.api.account.AccountRspModel;
import net.yc.web.imalachat.push.bean.api.account.LoginModel;
import net.yc.web.imalachat.push.bean.api.account.RegisterModel;
import net.yc.web.imalachat.push.bean.api.base.ResponseModel;
import net.yc.web.imalachat.push.bean.db.User;
import net.yc.web.imalachat.push.factory.UserFactory;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

/**
 * @author: lyc
 * @data: 2021-12-26  21:18
 * @Description: 172.0.0.1:8080/ImAlaChat/api/....
 */
@Path("/account")
public class AccountService extends BaseService{
    private static final String TAG = "AccountService";

    /**
     * 登录的入口
     * @param model
     * @return
     */
    @POST
    @Path("/login")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public ResponseModel<AccountRspModel> login(LoginModel model) {
        if (!LoginModel.check(model)) {
            // 返回参数异常
            return ResponseModel.buildParameterError();
        }

        User user = UserFactory.login(model.getAccount(), model.getPassword());
        if (user != null) {
            // 如果有携带pushId
            if (!Strings.isNullOrEmpty(model.getPushId())) {
                return bind(user, model.getPushId());
            }

            // 返回当前账户
            AccountRspModel rspModel = new AccountRspModel(user);
            return ResponseModel.buildOk(rspModel);
        } else {
            return ResponseModel.buildLoginError();
        }
    }

    /**
     * 注册的入口
     * @param model
     * @return
     */
    @POST
    @Path("/register")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public ResponseModel<AccountRspModel> register(RegisterModel model) {
        if (!RegisterModel.check(model)) {
            return ResponseModel.buildParameterError();
        }

        User user = UserFactory.findByPhone(model.getAccount().trim());
        if (user != null) {
            // 账户已存在
            return ResponseModel.buildHaveAccountError();
        }
        user = UserFactory.findByName(model.getName().trim());
        if (user != null) {
            // 名字已存在
            return ResponseModel.buildHaveNameError();
        }
        /**
         * 开始注册的逻辑
         */
        user = UserFactory.register(model.getAccount(), model.getPassword(), model.getName());

        if (user != null) {
            // 如果有携带pushId
            if (!Strings.isNullOrEmpty(model.getPushId())) {
                return bind(user, model.getPushId());
            }

            System.out.println(TAG + ": register() SUCCESS called with: model = [" + model + "]");

            // 返回当前的额账户
            AccountRspModel rspModel = new AccountRspModel(user);
            return ResponseModel.buildOk(rspModel);
        } else {
            // 注册异常
            return ResponseModel.buildRegisterError();
        }
    }

    // 绑定设备
    @POST
    @Path("/bind/{pushId}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    /**
     * 从请求头中获取token字段
     * 从URL中获取pushId
     */
    public ResponseModel<AccountRspModel> bind(@HeaderParam("token") String token,
                                               @PathParam("pushId") String pushId) {
        if (Strings.isNullOrEmpty(pushId)) {
            // 返回参数异常
            return ResponseModel.buildParameterError();
        }

        // 拿到个人信息
        User self = getSelf();
        return bind(self, pushId);
    }

    /**
     * 进行绑定的操作
     * @param self 自己
     * @param pushId 设备id
     * @return
     */
    private ResponseModel<AccountRspModel> bind (User self, String pushId) {
        User user = UserFactory.bindPushId(self, pushId);

        if (user == null) {
            // 如果绑定失败，则是服务器出现了问题
            return ResponseModel.buildServiceError();
        }

        // 返回当前账户，并且已经绑定
        AccountRspModel rspModel = new AccountRspModel(user, true);
        return ResponseModel.buildOk(rspModel);
    }

}
